package com.clinic.action.userlogin;

import org.springframework.beans.factory.annotation.Autowired;

import com.clinic.action.user.UserAction;
import com.clinic.constant.SystemMessage;
import com.clinic.entity.gui.GuiUserLogin;
import com.clinic.entity.param.SessionData;
import com.clinic.entity.param.SessionUser;
import com.clinic.logic.userlogin.UserLoginLogic;

public class UserLoginAction extends UserAction{
	
	@Autowired
	private UserLoginLogic userLoginLogic;

	private static final long serialVersionUID = -8430236643781490708L;
	private GuiUserLogin guiUserLogin;
	
	public String show(){
		return ActionResult.SUCCESS;
	}

	public String edit(){
		guiUserLogin = userLoginLogic.getLoginDetailsByUserId(id);
		
		if(guiUserLogin.isAvailable()) {
			return ActionResult.SUCCESS;
		}
		return ActionResult.FAILED;
	}
	
	public String update(){
		
		boolean isBlankValues = userLoginLogic.isBlankValues(guiUserLogin);
		if(isBlankValues) {
			addActionError("Login failed, Please fill all fields");	
		}
		
		boolean isValidUsername = userLoginLogic.isValidUsername(guiUserLogin);
		if(!isValidUsername) {
			addActionError("Login failed, Enter another username");
		}
		
		boolean isValidOldPassword = userLoginLogic.isValidOldPassword(guiUserLogin);
		if(!isValidOldPassword) {
			addActionError("Login failed, old password invalid");
		}
		
		boolean isValidNewPassword = userLoginLogic.isValidNewPassword(guiUserLogin);
		if(!isValidNewPassword) {
			addActionError("Login failed, please enter another new password");
		}
		
		boolean isValidConfirmation = userLoginLogic.isValidConfirmation(guiUserLogin);
		if(!isValidConfirmation) {
			addActionError("Login failed, invalid password confirmation");
		}
		
		if(hasActionErrors()) {
			return ActionResult.ERROR;
		} else {
			userLoginLogic.updateUserLogin(guiUserLogin);
		}
		return ActionResult.SUCCESS;
	} 
	
	public String signin() {
		return ActionResult.SUCCESS;
	}
	
	public String login() {
		
		boolean isBlankValues = userLoginLogic.isBlankLoginValues(guiUserLogin);
		if(isBlankValues) {
			addActionError(SystemMessage.BLANK_VALUES);
			return ActionResult.ERROR;
		}
		
		GuiUserLogin oGuiUserLogin = userLoginLogic.getLoginDetailsByUsername(guiUserLogin.getUsername());
		
		if(!oGuiUserLogin.isAvailable()) {
			addActionError(SystemMessage.INVALID_USERNAME);
			return ActionResult.ERROR;
		}
		
		if(!oGuiUserLogin.getPassword().equals(guiUserLogin.getPassword())) {
			addActionError(SystemMessage.INVALID_PASSWORD);
			return ActionResult.ERROR;
		}
		
		SessionUser sessionUser = userLoginLogic.getSessionUserByGuiUserLogin(oGuiUserLogin);
		setLoginUser(sessionUser);
		
	    return ActionResult.SUCCESS;
	}
	
	private void setLoginUser(SessionUser sessionUser) {
		SessionData sd = getSessionData();
		  if (sd == null) {
		   sd = new SessionData();
		  }
		  sd.setSessionUser(sessionUser);
		  setSessionData(sd);
	}
	
	public String logout() {
		  setLoginUser(null);
		  return ActionResult.SUCCESS;
	}
	
	public GuiUserLogin getGuiUserLogin() {
		return guiUserLogin;
	}
	public void setGuiUserLogin(GuiUserLogin guiUserLogin) {
		this.guiUserLogin = guiUserLogin;
	}

	public UserLoginLogic getUserLoginLogic() {
		return userLoginLogic;
	}

	public void setUserLoginLogic(UserLoginLogic userLoginLogic) {
		this.userLoginLogic = userLoginLogic;
	}

}
